package com.um.pasantiaum.truck;


import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.um.pasantiaum.DispatchActivity;
import com.um.pasantiaum.R;

public class TruckLocationActivity extends FragmentActivity {

	private GoogleMap mMap;
	private ParseObject truckEmpresa;
	private LocationListener ll;
	private LocationManager lm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_test_map);

		ParseQuery<ParseObject> pq = ParseQuery.getQuery("Truck");

		pq.whereEqualTo("truck", ParseUser.getCurrentUser());

		pq.getFirstInBackground(new GetCallback<ParseObject>() {

			@Override
			public void done(ParseObject object, ParseException e) {
				if (e == null) {
					if (object == null) {
						Toast.makeText(TruckLocationActivity.this,
								"No se puede conectar al servidor",
								Toast.LENGTH_LONG).show();
					} else {
						truckEmpresa = object;
					}
				} else {
					Toast.makeText(TruckLocationActivity.this, e.getMessage(),
							Toast.LENGTH_LONG).show();
				}
			}
		});

		lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		ll = new myLocationListener();

		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1 * 10 * 1000,
				0, ll);

		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
				1 * 4 * 1000, 0, ll);
	}

	class myLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location location) {
			if (location != null) {
				ParseGeoPoint point = new ParseGeoPoint(location.getLatitude(),
						location.getLongitude());

				mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
						new LatLng(location.getLatitude(), location
								.getLongitude()), 17.0f));

				if (location.getAccuracy() > 100)
					return;

				if (truckEmpresa != null) {
					truckEmpresa.put("position", point);
					Date date = new Date();
					truckEmpresa.put("dateText", date.toString());

					truckEmpresa.saveInBackground();
					Log.d("WOOHOOOOOOO", "SE DEBERIA HABER GUARDADO");
				} else {
					Toast.makeText(TruckLocationActivity.this,
							"No se puede conectar al servidor",
							Toast.LENGTH_LONG).show();
				}
			}
		}

		@Override
		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProviderEnabled(String arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			// TODO Auto-generated method stub

		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		setUpMapIfNeeded();
		// setUpLocationClientIfNeeded();
		// mLocationClient.connect();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.log_out_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.log_out:
			ParseUser.logOut();
			// Start and intent for the dispatch activity
			Intent intent = new Intent(TruckLocationActivity.this,
					DispatchActivity.class);
			startActivity(intent);
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void setUpMapIfNeeded() {
		// Do a null check to confirm that we have not already instantiated the
		// map.
		if (mMap == null) {
			// Try to obtain the map from the SupportMapFragment.
			mMap = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map)).getMap();
			// Check if we were successful in obtaining the map.
			if (mMap != null) {

				mMap.setMyLocationEnabled(true);
				// mMap.setOnMyLocationButtonClickListener(this);
			}
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		lm.removeUpdates(ll);
		finish();
	}
	
}
